﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Krilib.Data;
using System.Windows.Forms;

namespace Krile.Data
{
    static internal class DefaultKeyAssigns
    {
        internal static void SelectAbove(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.SetSelectStatus(Krile.Forms.MainForm.Main.SelectTargets.Above);
            Core.FormManager.MainForm.EnsureVisible();   
        }

        internal static void SelectBelow(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.SetSelectStatus(Krile.Forms.MainForm.Main.SelectTargets.Below);
            Core.FormManager.MainForm.EnsureVisible();
        }

        internal static void SelectNewAbove(Status selected, Keys pressed)
        {
            if (!Core.FormManager.MainForm.SetSelectStatus(Krile.Forms.MainForm.Main.SelectTargets.AboveNew))
            {
                MessageBox.Show(Lang.KeyAssign.msg_NothingNewAbove, "Move error",
                     MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                Core.FormManager.MainForm.EnsureVisible();
        }

        internal static void SelectNewBelow(Status selected, Keys pressed)
        {
            if (!Core.FormManager.MainForm.SetSelectStatus(Krile.Forms.MainForm.Main.SelectTargets.BelowNew))
            {
                MessageBox.Show(Lang.KeyAssign.msg_NothingNewBelow, "Move error",
                     MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                Core.FormManager.MainForm.EnsureVisible();
        }

        internal static void ScrollAbove(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.SetScrollState(Krile.Forms.MainForm.Main.SelectTargets.Above);
        }

        internal static void ScrollBelow(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.SetScrollState(Krile.Forms.MainForm.Main.SelectTargets.Below);
        }

        internal static void ScrollTop(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.SetScrollState(Krile.Forms.MainForm.Main.SelectTargets.Top);
        }

        internal static void ScrollEnd(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.SetScrollState(Krile.Forms.MainForm.Main.SelectTargets.End);
        }

        internal static void SelectNewestStatus(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.SetSelectStatus(Krile.Forms.MainForm.Main.SelectTargets.Newest);
            Core.FormManager.MainForm.EnsureVisible();
        }

        internal static void OpenPostbox(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.SetPostboxState(true);
        }

        internal static void ClosePostbox(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.SetPostboxState(false);
        }

        internal static void ShowConfig(Status selected, Keys pressed)
        {
            if (Core.FormManager.MainForm.Visible)
                Core.FormManager.MainForm.ShowConfig();
            else
                using (var cog = new Forms.ConfigForm.Config())
                {
                    cog.ShowDialog();
                }
        }

        internal static void AddNewTab(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.CreateTab();
        }

        internal static void AddQuickTab(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.CreateQuickTab();
        }

        internal static void AddTabFromUser(Status selected, Keys pressed)
        {
            if (selected == null)
                return;
            var baseData = ((Forms.MainForm.Controls.XTab.XTabTLItem)Core.FormManager.MainForm.TabControl.SelectedItem).TabData;
            var newData = new Krile.Data.TabData();
            newData.Name = String.Format(Lang.QuickTab.TabImportStr, selected.user.id, baseData.Name);
            newData.UnNotify = baseData.UnNotify;
            List<SearchDataBase> sdbArray = new List<SearchDataBase>();
            if (baseData.ContainSearchData != null)
                sdbArray.Add(baseData.ContainSearchData);
            sdbArray.Add(new Data.UserIdSearchData(selected.user.id));
            newData.ContainSearchData = new SearchDataCluster(sdbArray.ToArray());
            Core.FormManager.MainForm.TabControl.AddTab(
                new Krile.Forms.MainForm.Controls.XTab.XTabTLItem(
                    Core.FormManager.MainForm.TabControl,
                    newData));
        }

        internal static void AddTabFromService(Status selected, Keys pressed)
        {
            if (selected == null)
                return;
            var baseData = ((Forms.MainForm.Controls.XTab.XTabTLItem)Core.FormManager.MainForm.TabControl.SelectedItem).TabData;
            var newData = new Krile.Data.TabData();
            newData.Name = String.Format(Lang.QuickTab.TabImportStr, "Service " + selected.ParentService.Name, baseData.Name);
            newData.UnNotify = baseData.UnNotify;
            List<SearchDataBase> sdbArray = new List<SearchDataBase>();
            if (baseData.ContainSearchData != null)
                sdbArray.Add(baseData.ContainSearchData);
            sdbArray.Add(new Data.ServiceIdSearchData(selected.ParentService.Id));
            newData.ContainSearchData = new SearchDataCluster(sdbArray.ToArray());
            Core.FormManager.MainForm.TabControl.AddTab(
                new Krile.Forms.MainForm.Controls.XTab.XTabTLItem(
                    Core.FormManager.MainForm.TabControl,
                    newData));
        }

        internal static void EditSelectedTab(Status selected, Keys pressed)
        {
            var item = Core.FormManager.MainForm.TabControl.SelectedItem as Forms.MainForm.Controls.XTab.XTabTLItem;
            if (item != null)
                item.EditThis(null, null);
        }

        internal static void CloseSelectedTab(Status selected, Keys pressed)
        {
            var item = Core.FormManager.MainForm.TabControl.SelectedItem as Forms.MainForm.Controls.XTab.XTabTLItem;
            if (item != null)
                item.CloseThis(null, null);
        }

        internal static void ToggleTabLock(Status selected, Keys pressed)
        {
            var item = Core.FormManager.MainForm.TabControl.SelectedItem as Forms.MainForm.Controls.XTab.XTabTLItem;
            if (item != null)
                item.ToggleLock(null, null);
        }

        internal static void ToggleGlobalTL(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.ToggleRecvGTL(null, null);
        }

        internal static void RefreshTimeline(Status selected, Keys pressed)
        {
            foreach (var svc in Core.PluginManager.GetServiceEnumerator())
            {
                svc.ParentAgent.Status.ReceiveStatuses();
            }
        }

        internal static void SetAlreadyReadedAll(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.MAllReaded_Click(null, null);
        }

        internal static void ToggleTopmost(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.ToggleTopMost(null, null);
        }
    
        internal static void SelectRightTab(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.TabControl.SelectNextTab();
        }

        internal static void SelectLeftTab(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.TabControl.SelectPrevTab();
        }

        internal static void DeselectTimeline(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.SetSelectStatus(Krile.Forms.MainForm.Main.SelectTargets.Deselect);
        }

        internal static void EnsureVisibleSelectedStatus(Status selected, Keys pressed)
        {
            Core.FormManager.MainForm.EnsureVisible();
        }

        internal static void ShowUserPage(Status selected, Keys pressed)
        {
            if (selected == null) return;
            Subsystem.CommonOperation.CallBrowser(selected.user.profileUri);
        }

        internal static void ShowStatusPage(Status selected, Keys pressed)
        {
            if (selected == null) return;
            Subsystem.CommonOperation.CallBrowser(selected.statusPage);
        }

        internal static void JumpInReplyTo(Status selected, Keys pressed)
        {
            if (selected == null) return;
            var uid = selected.replyData.ReplyOnClick();
            if (uid > 0)
            {
                if (!Core.FormManager.MainForm.SelectStatusByUID(uid, true))
                    MessageBox.Show(Lang.Renderer.JumpToReplyError, "Jump to reply",
                         MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            Core.FormManager.MainForm.EnsureVisible();
        }

        internal static void CopyStatus(Status selected, Keys pressed)
        {
            if (selected == null) return;
            Clipboard.SetText(selected.text.Original);
            Core.FormManager.MainForm.SetStatusText(Lang.StatusText.Copied, 3);
        }

        internal static void CopyUserID(Status selected, Keys pressed)
        {
            if (selected == null) return;
            Clipboard.SetText(selected.user.id);
            Core.FormManager.MainForm.SetStatusText(Lang.StatusText.Copied, 3);
        }

        internal static void DefaultReply(Status selected, Keys pressed)
        {
            if (selected == null) return;
            if (!selected.ParentAgent.Status.AutoCreateReplyOperationTriggers) return;
            Core.FormManager.MainForm.InputControlManager.SetReplyTo(selected.UID);
        }

        internal static void DefaultFav(Status selected, Keys pressed)
        {
            if (selected == null) return;
            if (!selected.ParentAgent.Status.AutoCreateFavOperationTriggers) return;


            Action act = new Action(() =>
            {
                Core.FormManager.MainForm.SetStatusText(Lang.StatusText.Favouring);
                Core.FormManager.MainForm.RefreshTL(false);
                var or = selected.ParentAgent.Status.SetFavorite(selected.UID, !selected.favored);
                if (or.Succeeded)
                {
                    selected.favored = !selected.favored;
                }
                else
                {
                    Subsystem.Debugger.AddReport(new Krilib.Bridges.DebugReport("Fav operation was failed at " + selected.UID.ToString()));
                    Core.FormManager.MainForm.SetStatusText(Lang.StatusText.FavError, 3);
                }
                Core.FormManager.MainForm.RefreshTL(true);
                Core.FormManager.MainForm.SetStatusText(Lang.StatusText.Favored, 3);
            });
            act.BeginInvoke((iar) => ((Action)iar.AsyncState).EndInvoke(iar), act);
        }
    }
}
